package com.luli.controllor;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 陆离
 * 2022/6/30 15:10:30
 */
public class JucController {
    private Lock lock = new ReentrantLock();//新建锁
    Condition condition1 = lock.newCondition();  //新建监视器
    Condition condition2 = lock.newCondition();
    Condition condition3 = lock.newCondition();
    private int number = 1;
    public void Test1() {
            lock.lock();  //开启锁
            try {
                while (number != 1){
                    condition1.await();   //如果number!=1,那么监视器一等待,否则number = 2;的情况,就唤醒监视器二
                }
                System.out.println(Thread.currentThread().getName() + "1在等待");
                number = 2;
                condition2.signal();
                System.out.println("condition2被唤醒");
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                lock.unlock();  //释放锁
            }
        }
        //同上
        public void Test2() {
            lock.lock();  //开启锁
            try {
                while (number != 2){
                    condition2.await();
                }
                System.out.println(Thread.currentThread().getName() + "2在等待");
                number = 3;
                condition3.signal();
                System.out.println("condition3被唤醒");
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                lock.unlock();  //释放锁
            }
        }

    public static void main(String[] args) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date1 = sdf.parse("2009-12-31");
        Date date2 = sdf.parse("2019-01-31");
        System.out.println("date1 : " + sdf.format(date1));
        System.out.println("date2 : " + sdf.format(date2));
        if (date1.compareTo(date2) > 0) {
            System.out.println("Date1 时间在 Date2 之后");
        } else if (date1.compareTo(date2) < 0) {
            System.out.println("Date1 时间在 Date2 之前");
        } else if (date1.compareTo(date2) == 0) {
            System.out.println("Date1 时间与 Date2 相等");
        } else {
            System.out.println("程序怎么会运行到这里?正常应该不会");
        }
    }
}
